package com.intel.two.interceptor;


import com.intel.two.pojo.user.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


//登录拦截器
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      HttpSession session = request.getSession();

      User userLogin = (User) session.getAttribute("user");
      System.out.println("拦截登录用户"+userLogin);
//        if (userLogin!=null) {
//      if(!"".equals(userLogin.getAccount())&&userLogin.getAccount()!=null) {
////           放行
//            return true;
//      }}


        //登录页面也会放行
        if (request.getRequestURI().contains("login.do")){
            return true;
        }

//        //说明我在提交登录
        if (request.getRequestURI().contains("loginout.do")){
            return true;
        }

        if (userLogin != null){
            return true;
        }



        //判断什么情况下没有登录
        request.getRequestDispatcher("http://127.0.0.1:5501/login.html").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
